import 'package:flutter/material.dart';

class FieldPage extends StatefulWidget {
  const FieldPage({Key? key}) : super(key: key);

  @override
  _FieldPageState createState() => _FieldPageState();
}

class _FieldPageState extends State<FieldPage> {
  //优雅的通过Controller双向绑定，可以提前赋值，也可以自动在提交时获取值
  var _username=new TextEditingController();
  var _password=new TextEditingController();
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    //设置初始值
    _username.text='塔睿';
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('hello'),
      ),
      body:Padding(
        padding: EdgeInsets.all(20),
        child: Column(
          children: <Widget>[
            TextField(
              decoration: InputDecoration(
                hintText: '请输入用户名',
              ),
              //设置controller双向绑定后，提交时的onPressed直接获取文本框当前值
              controller: _username,
            ),
            SizedBox(height: 10),
            TextField(
              obscureText: true,
              decoration: InputDecoration(
                hintText: '请输入密码',
              ),
              //设置controller双向绑定后，提交时的onPressed直接获取文本框当前值
              controller: _password,
            ),
            SizedBox(height: 20),
            Container(
              width: double.infinity,
              child: ElevatedButton(
                child: Text('提交'),
                onPressed: (){
                  print('账号:${_username.text}---密码:${_password.text}');
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}
